/*	
	
	Copyright (c) 2007-2009 Ryan Christiansen
	
	This software is provided 'as-is', without any express or implied
	warranty. In no event will the authors be held liable for any damages
	arising from the use of this software.
	
	Permission is granted to anyone to use this software for any purpose,
	including commercial applications, and to alter it and redistribute it
	freely, subject to the following restrictions:
	
	1. The origin of this software must not be misrepresented; you must not
	claim that you wrote the original software. If you use this software
	in a product, an acknowledgment in the product documentation would be
	appreciated but is not required.
	
	2. Altered source versions must be plainly marked as such, and must not be
	misrepresented as being the original software.
	
	3. This notice may not be removed or altered from any source
	distribution.
	
*/
package funit.gui
{
	import funit.core.ITest;
	import funit.core.ITestListener;
	import funit.core.TestRunner;
	import funit.listeners.events.RunFinishErrorEvent;
	import funit.listeners.events.RunFinishEvent;
	import funit.listeners.events.RunStartEvent;
	import funit.listeners.events.SuiteFinishEvent;
	import funit.listeners.events.SuiteStartEvent;
	import funit.listeners.events.TestFinishEvent;
	import funit.listeners.events.TestOutputEvent;
	import funit.listeners.events.TestStartEvent;
	import funit.listeners.events.UnhandledErrorEvent;
	
	import mx.containers.Panel;
	import mx.controls.HRule;
	import mx.controls.Label;
	import mx.controls.ProgressBar;
	import mx.controls.ProgressBarLabelPlacement;
	import mx.controls.ProgressBarMode;
	import mx.controls.Spacer;
	
	[Event(name="runStart", type="funit.listeners.events.RunStartEvent")]
	[Event(name="runFinish", type="funit.listeners.events.RunFinishEvent")]
	[Event(name="runFinishError", type="funit.listeners.events.RunFinishErrorEvent")]
	
	[Event(name="testStart", type="funit.listeners.events.TestStartEvent")]
	[Event(name="testFinish", type="funit.listeners.events.TestFinishEvent")]
	
	[Event(name="suiteStart", type="funit.listeners.events.SuiteStartEvent")]
	[Event(name="suiteFinish", type="funit.listeners.events.SuiteFinishEvent")]
	
	[Event(name="unhandledError", type="funit.listeners.events.UnhandledErrorEvent")]
	
	[Event(name="testOutput", type="funit.listeners.events.TestOutputEvent")]
	
	public class SimpleTestRunner extends Panel
	{
		protected var _title:Label;
		protected var _label:Label;
		protected var _progressBar:ProgressBar;
		
		protected var _testRunLabel:Label;
		protected var _testIgnoreLabel:Label;
		protected var _testFailureLabel:Label;
		
		protected var _runner:TestRunner;
		
		protected var _currentCount:int;
		protected var _testRunCount:int;
		protected var _testIgnoreCount:int;
		protected var _testFailureCount:int;
		
		
		/**
		 * 
		 */
		public function SimpleTestRunner()
		{
			
		}
		
		
		/**
		 * 
		 */
		override public function initialize() : void
		{
			super.initialize();
			
			_runner = new TestRunner();
			_runner.addEventListener(RunStartEvent.RUN_START, runStartedHandler);
			_runner.addEventListener(RunFinishEvent.RUN_FINISH, runFinishedHandler);
			_runner.addEventListener(RunFinishErrorEvent.RUN_FINISH_ERROR, runFinishedErrorHandler);
			_runner.addEventListener(TestStartEvent.TEST_START, testStartedHandler);
			_runner.addEventListener(TestFinishEvent.TEST_FINISH, testFinishedHandler);
			_runner.addEventListener(SuiteStartEvent.SUITE_START, suiteStartedHandler);
			_runner.addEventListener(SuiteFinishEvent.SUITE_FINISH, suiteFinishedHandler);
			_runner.addEventListener(UnhandledErrorEvent.UNHANDLED_ERROR, unhandledErrorHandler);
			_runner.addEventListener(TestOutputEvent.TEST_OUTPUT, testOutputHandler);
		}
		
		/**
		 * 
		 */
		override protected function createChildren() : void
		{
			super.createChildren();
			
			// ------------------------------
			// Title Section
			// ------------------------------
			
			_title = new Label();
			_title.setStyle("color", getStyle("color"));
			_title.setStyle("fontSize", "16");
			_title.setStyle("fontWeight", "bold");
			_title.text = "FUnit Runner";
			
			var hrule:HRule = new HRule();
			hrule.percentWidth = 100;
			
			addChild(_title);
			addChild(hrule);
			
			// ------------------------------
			// Progress Section
			// ------------------------------
			
			var spacer:Spacer = new Spacer();
			spacer.height = 10;
			
			_label = new Label();
			_label.setStyle("color", getStyle("color"));
			_label.setStyle("fontWeight", "bold");
			_label.text = "";
			
			_progressBar = new ProgressBar();
			_progressBar.setStyle("color", getStyle("color"));
			_progressBar.setStyle("barColor", 0x00FF00);
			_progressBar.mode = ProgressBarMode.MANUAL;
			_progressBar.labelPlacement = ProgressBarLabelPlacement.RIGHT;
			_progressBar.label = "%1/%2";
			
			addChild(spacer);
			
			addChild(_label);
			addChild(_progressBar);
			
			// ------------------------------
			// Result Section
			// ------------------------------
			
			var spacer2:Spacer = new Spacer();
			spacer2.height = 10;
			
			_testRunLabel = new Label();
			_testRunLabel.percentWidth = 100;
			_testRunLabel.setStyle("color", getStyle("color"));
			_testRunLabel.setStyle("fontWeight", "bold");
			
			_testIgnoreLabel = new Label();
			_testIgnoreLabel.percentWidth = 100;
			_testIgnoreLabel.setStyle("color", getStyle("color"));
			_testIgnoreLabel.setStyle("fontWeight", "bold");
			
			_testFailureLabel = new Label();
			_testFailureLabel.percentWidth = 100;
			_testFailureLabel.setStyle("color", getStyle("color"));
			_testFailureLabel.setStyle("fontWeight", "bold");
			
			addChild(spacer2);
			
			addChild(_testRunLabel);
			addChild(_testIgnoreLabel);
			addChild(_testFailureLabel);
		}
		
		/**
		 * 
		 */
		override protected function commitProperties() : void
		{
			super.commitProperties();
			
			if (_testFailureCount > 0)
			{
				_progressBar.setStyle("barColor", 0xFF0000);
			}
			
			_progressBar.setProgress(_currentCount, _runner.testCount);
			
			_testRunLabel.text		= "Tests Run:\t" + _testRunCount;
			_testIgnoreLabel.text	= "Ignored:\t" + _testIgnoreCount;
			_testFailureLabel.text	= "Failures:\t" + _testFailureCount;
		}
		
		/**
		 * @param listener
		 */
		public function run( listener:ITestListener = null ) : void
		{
			_currentCount = 0;
			_testRunCount = 0;
			_testIgnoreCount = 0;
			_testFailureCount = 0;
			
			_label.text = "Running Tests...";
			
			_runner.run(listener);
			
			invalidateProperties();
		}
		
		/**
		 * @param event
		 */
		private function runStartedHandler( event:RunStartEvent ) : void
		{
			dispatchEvent( event.clone() );
		}
		
		/**
		 * @param event
		 */
		private function runFinishedHandler( event:RunFinishEvent ) : void
		{
			_label.text = "Tests Complete";
			
			dispatchEvent( event.clone() );
		}
		
		/**
		 * @param event
		 */
		private function runFinishedErrorHandler( event:RunFinishErrorEvent ) : void
		{
			dispatchEvent( event.clone() );
		}
		
		/**
		 * @param event
		 */
		private function testStartedHandler( event:TestStartEvent ) : void
		{
			dispatchEvent( event.clone() );
		}
		
		/**
		 * @param event
		 */
		private function testFinishedHandler( event:TestFinishEvent ) : void
		{
			_currentCount++;
			
			if (event.result.executed)
			{
				_testRunCount++;
				
				if (event.result.isFailure)
				{
					_testFailureCount++;
				}
			}
			else
			{
				_testIgnoreCount++;
			}
			
			invalidateProperties();
			
			dispatchEvent( event.clone() );
		}
		
		/**
		 * @param event
		 */
		private function suiteStartedHandler( event:SuiteStartEvent ) : void
		{
			dispatchEvent( event.clone() );
		}
		
		/**
		 * @param event
		 */
		private function suiteFinishedHandler( event:SuiteFinishEvent ) : void
		{
			dispatchEvent( event.clone() );
		}
		
		/**
		 * @param event
		 */
		private function unhandledErrorHandler( event:UnhandledErrorEvent ) : void
		{
			dispatchEvent( event.clone() );
		}
		
		/**
		 * @param event
		 */
		private function testOutputHandler( event:TestOutputEvent ) : void
		{
			dispatchEvent( event.clone() );
		}
		
		
		/**
		 * 
		 */
		public function get test() : ITest
		{
			return _runner.test;
		}
		
		public function set test( value:ITest ) : void
		{
			if (value != null)
			{
				_label.text = "Tests Loaded";
				
				_runner.load(value);
			}
			else
			{
				_runner.unload();
			}
		}
		
	}
	
}
